import itertools
from functools import reduce
from itertools import chain


class BaseList(object):

    @classmethod
    def list_to_nest_list(cls, listinput, num):
        """
        将一个list转化成二维的嵌套list 并指定子list的长度
        :param listinput:
        :param num: 二维list中子list的长度
        :return:
        """
        return [listinput[i:i + 5] for i in range(0, len(listinput), num)]

    @classmethod
    def nest_list_to_list(cls, listinput, var):
        """
        将二维list转化成一维的
        :param listinput:
        :return:
        """
        t = []
        assert var in ["1", "2", "3", "4", "5"],ValueError("给出的值不在范围内")
        return {
            "1": list(chain(*listinput)),
            "2": list(itertools.chain.from_iterable(listinput)),
            "3": [t.extend(i) for i in listinput],
            "4": sum(listinput, []),
            "5": reduce(lambda x, y: x + y, listinput)
        }.get(var)
